﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using InductBase;

namespace SharpBolt.Collision.CollisionSprite
{
    public class CollisionSpriteSource
    {
        public class Frame
        {
            public List<CollShape> Shapes;
        }

        internal class Sequence
        {
            public Sequence(string name)
            {
                Name = name;
                Frames = new List<Frame>();
            }
            public string Name { get; set; }
            public List<Frame> Frames { get; set; }
        }

        internal List<Sequence> Sequences { get; private set; }
                
        public static CollisionSpriteSource LoadSprite(string name)
        {
            CollisionSpriteSource sprite = new CollisionSpriteSource();
            sprite.Sequences = new List<Sequence>();

            string description = Engine.Miscs.LoadText(name + ".col");

            var lines = description.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);

            Sequence CurrentSequence = null;
            Frame CurrentFrame = null;

            foreach (var line in lines)
            {
                string[] args = line.Split(new char[] { '\t', ' ' }, StringSplitOptions.RemoveEmptyEntries);

                if (args[0] == "s")
                {
                    CurrentSequence = new Sequence(args[1]);
                    sprite.Sequences.Add(CurrentSequence);
                }
                else if (args[0] == "f")
                {
                    CurrentFrame = new Frame() { Shapes = new List<CollShape>() };
                    CurrentSequence.Frames.Add(CurrentFrame);
                }
                else if (args[0] == "c")
                {
                    CollShape shape = new CollShape();
                    shape.Tag = args[1];
                    shape.Size = Vec2.Parse(args[2]);
                    shape.Transformation = Transformation.Parse(args[3]);
                    CurrentFrame.Shapes.Add(shape);
                }
            }


            return sprite;
        }
    }
}
